//
//  AppDelegate.swift
//  大深圳微博01
//
//  Created by shenzhenIOS on 16/4/8.
//  Copyright © 2016年 shenzhenIOS. All rights reserved.
//

import UIKit

/*
    微博数据缓存流程:
        1.查看本地数据库是否保存数据
        2.如果本地数据有数据直接使用本地数据库的数据
        3.本地没有数据,发送网络请求,去服务器加载数据
        4.将服务器返回的数据保存到本地
        5.在将服务器返回的数据,返回给别人处理
*/

/*
    1.plist: NSArray/NSDictionary: writeToFile
    2.偏好设置: NSUserDefaults: key -> value, 小数据, 国际化 12语言
    3.NSCoding: 归档和解档: 对象
    4.SQLite: 数据
    5.CoreData: SQLite, 内存,xml
*/

// 可选理解为红包

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        setupAppearance()
        
        HMSQLiteManager.sharedManager
        
        // 测试账号
        let account = HMUserAccountViewModel.sharedInstance.userAccount
        HMPrint("账号: \(account)")

        // 创建窗口
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
//        let mainVC = HMMainViewController()
        
        // 设置root控制器
        window?.rootViewController = defaultViewController()
        
        // TODO: 测试
//        window?.rootViewController = HMNewFeatureViewController()
        
        // 设置为主窗口
        // 当window有值得时候才往后面执行
        window?.makeKeyAndVisible()
        
        // 一定要记得去掉
//        HMPrint("isNewVersion: \(isNewVersion())")
        
        return true
    }
    
    /// 切换控制器,搞一个统一的切换方法,到时候调用方便
    func switchViewController(controller: UIViewController) {
        window?.rootViewController = controller
    }
    
    /// 进入程序设置默认控制器
    private func defaultViewController() -> UIViewController {
        if !HMUserAccountViewModel.sharedInstance.userLogin {
            // 没有登录
            return HMMainViewController()
        }
        
        // 表示已经登录
        let newVersion = isNewVersion()
        return newVersion ? HMNewFeatureViewController() : HMWelcomeViewController()
    }
    
    /// 设置UIBarButtonItem皮肤
    private func setupAppearance() {
        let barButtonItemAppearance = UIBarButtonItem.appearance()
        barButtonItemAppearance.tintColor = UIColor.orangeColor()
    }
    
    /// 是否是新版本
    ///
    /// - returns: 是否是新版本
    private func isNewVersion() -> Bool {
        // 1.获取当前版本号
        let currentVersion = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as! String
        
        // 2.获取上一次的版本号
        let sandboxKey = "sandboxKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxKey)
        
        // 3.对比
        let newVersion = (currentVersion != sandboxVersion)
        
//        HMPrint("currentVersion: \(currentVersion), sandboxVersion: \(sandboxVersion)")
        
        // 4.是新版本,保存新的版本号
        if newVersion {
            NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: sandboxKey)
            NSUserDefaults.standardUserDefaults().synchronize()
        }
        
        // 5.返回比较的结果
        return newVersion
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // 清楚缓存
        HMStatusDAL.sharedDAL.deleteCacheStatus()
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

